<!--
 * @Author: Dorange.Wang
 * @Date: 2021-04-20 14:16:40
 * @LastEditors: wangming
 * @LastEditTime: 2021-09-28 15:20:55
 * @Description: file content
-->
<template>
  <el-dialog :visible="showVisible" center :show-close="false" width="600px">
    <div class="b g3 f16 tl" slot="title">
      <div class="ks-row-between pl15 pr15">
        <span>{{ isNew ? "新建报表" : "编辑报表" }}</span>
        <span
          class="el-icon-close g9 f26 poi"
          @click="showVisible = false"
        ></span>
      </div>
    </div>
    <el-form
      @submit.native.prevent
      class="pl30 pr30"
      :model="formData"
      ref="form"
      label-width="120px"
    >
      <el-form-item
        v-if="isNew"
        label="报表名称"
        prop="fileName"
        :rules="[
          { required: true, message: '报表名称不能为空', trigger: 'change' },
        ]"
      >
        <el-input
          v-model="formData.fileName"
          style="width: 400px"
          placeholder="请输入报表名称"
          :maxlength="64"
          show-word-limit
        />
      </el-form-item>

      <el-form-item
        v-else
        label="报表名称"
        prop="preFileName"
        :rules="[
          { required: true, message: '报表名称不能为空', trigger: 'change' },
        ]"
      >
        <el-input
          disabled
          v-model="formData.preFileName"
          style="width: 400px"
          placeholder="请输入报表名称"
          :maxlength="64"
          show-word-limit
        />
      </el-form-item>

      <el-form-item label="报表描述">
        <el-input
          type="textarea"
          v-model="formData.reportDesc"
          style="width: 400px"
          placeholder="请输入报表描述"
          :maxlength="200"
          show-word-limit
        ></el-input>
      </el-form-item>
    </el-form>

    <div slot="footer" class="tr">
      <el-button @click="showVisible = false">取 消</el-button>
      <el-button type="primary" @click="submit">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { postHmureport,putHmureport } from '@/apis/paas/HmUreportController.js'

export default {
  name: "ReportForm",
  data() {
    return {
      showVisible: false,
      formData: {},
    };
  },

  computed: {
    isNew() {
      return !this.formData.id;
    },
  },

  created() {},

  methods: {
    addForm() {
      this.formData = {};
      this.showVisible = true;
      this.$nextTick(() => {
        this.$refs.form.clearValidate();
      });
    },

    editForm(record = {}) {
      this.showVisible = true;
      this.formData = {
        ...record,
      };
      this.$nextTick(() => {
        this.$refs.form.clearValidate();
      });
    },

    async submit() {
      try {
        await this.$refs.form.validate();
        const { id, fileName, reportDesc } = this.formData;
        let params = {};
        let request = null;
        if (this.isNew) {
          params = { fileName, reportDesc };
          request = postHmureport;
        } else {
          params = { id, reportDesc };
          request = putHmureport;
        }
        for (const key in params) {
          params[key] =
            typeof params[key] === "string" ? params[key].trim() : params[key];
        }
        request(params).then((res) => {
          this.showVisible = false;
          this.$emit("ok", res);
        });
      } catch (error) {
        console.log(error);
      }
    },
  },
};
</script>

<style lang="scss" scoped>
</style>
